﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Grishko.DataTier.SqlSentence;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Agency;
using Grishko.Tools;
using OutdoorLib.BaseEntity.Infrastructure;

/// <summary>
/// создание / редактирование ильтра 
/// @ID= pNerRec+ListType  если создание
/// @ID=GUID - существующий фильтр 
/// </summary>
public partial class FilterEditor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyInit();
        if (!IsPostBack)
        {
            if (GlobalConstants.IsNewRecord(CurrentId))
            {
                txtCaption.Text = "Новая выборка";
                Save();
            }

            MyBind();
        }

        ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), "HideAlert();", true);        
    }

    private Filter GetFilterAsDataSource()
    {
        return _Filter;
    }
    
    protected void MyInit()
    {
        ctlFilterEditor1.DataSource = GetFilterAsDataSource;
        ctlFilterEditor1.SaveMethod = Save;
        
    }
    
    protected void MyBind()
    {        
        
        DataBind();
    }


    
    protected ScriptEngine  GetListWhere()
    {
        return _Filter.Where;
    }

    protected  string ListTagName
    {
        get
        {
            if (String.IsNullOrEmpty(Request.QueryString["ListEntityType"]))
                return  _Filter.TagName ;

            return Request.QueryString["ListEntityType"];
        }

    }

    protected bool  IsUserFilter
    {
        get
        {
            return rbFilterType.Items[0].Selected ;
        }
    }
    
    protected string CurrentId
    {
        get
        {   
            return Request .QueryString ["ID"];
        }
    }

    protected string TagName
    {
        get
        {
            return Request.QueryString["TagName"];
        }
    }


    protected wsAgency WS
    {
        get
        {
         return  new OutdoorLib.BaseEntity.WS.wsAgency();
        }
    }
    
    private Filter __Filter = null; 
  
    protected  Filter _Filter
    {
        get
        {
            if (null != __Filter)
                    return __Filter;

            if (GlobalConstants.IsNewRecord(CurrentId))
            {
                __Filter = AppEnvironment.FilterSource.GetNewListWhere(CurrentId,TagName);
            }
            else
                __Filter = AppEnvironment.FilterSource.Get(CurrentId);                    
            
            
            
            return __Filter;
        }
        
    }

    public void ReadForm()
    {
        _Filter.Caption = txtCaption.Text;
        _Filter.Describe = txtComment.Text;        
        _Filter.ListCategory = (IsUserFilter ? Filter.ListEntityCategory.User : Filter.ListEntityCategory.Client); 
    }
    

    protected bool Save()
    {
        ReadForm();
        bool bIsNewRecord = GlobalConstants.IsNewRecord(CurrentId);

        AppEnvironment.FilterSource.Save(_Filter); 

        if (bIsNewRecord)
        {
            QueryStringTools.RefreshPage("ID=" + _Filter.ID);
        }
        return true;
    }

    protected void CommonFilterSave()
    {        
    }

    protected void UserFilterSave()
    {

    }
  
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!this.IsValid)
                        return;
         if( Save())
             QueryStringTools.RefreshPageWitchReturnUrl();           

    }
}
